
<script>
// @ is an alias to /src

import qs from 'qs'
import * as dd from 'dingtalk-jsapi'
import { Toast } from 'vant'

export default {
  name: 'index',
  data () {
    return {
      corpId: '',
      code: '',
      wxuserid: '',
      wxgzuserid: ''
    }
  },
  created: function () {
    let ua = window.navigator.userAgent.toLowerCase()
    //console.log(ua)
    let logintype = 'wechat'
    // let tourl = window.location.href
    // alert('url:' + tourl)
    var r = window.location.search.substr(1).match(new RegExp('(^|&)corpId=([^&]*)(&|$)'))
    if (r != null) {
      this.corpId = unescape(r[2])
    }

    var w = window.location.search.substr(1).match(new RegExp('(^|&)wxuserid=([^&]*)(&|$)'))

    if (w != null) {
      this.wxuserid = unescape(w[2])
    }

    var gz = window.location.search.substr(1).match(new RegExp('(^|&)wxgzuserid=([^&]*)(&|$)'))
    if (gz != null) {
      this.wxgzuserid = unescape(gz[2])
    }

    // let clttype = 'android'

    if (ua.indexOf('wxwork') !== -1) {
      logintype = 'wxwork'
    } else if (ua.indexOf('dingtalk') !== -1) {
      logintype = 'dingding'
    } else if (ua.indexOf('micromessenger') !== -1) {
      logintype = 'wechat'
    } else {
      logintype = 'browser'
    }
    // if (logintype === 'browser') {
    //   alert('请在钉钉或微信端登录!')
    //   this.$router.go(-1)
    // }
    // if (ua.indexOf('iphone') !== -1) {
    //   clttype = 'ios'
    // }

    if (logintype === 'dingding') {
      if (this.corpId !== '') {
        // 说明需要回调
        // alert(this.corpId)
        // Toast('准备调用dd的code')

        dd.ready(() => {
          // Toast('dd ready')
          dd.runtime.permission.requestAuthCode({
            corpId: this.corpId, // 企业id
            onSuccess: (info) => {
              this.code = info.code
              // Toast('code:' + this.code)

              // 通过该免登授权码可以获取用户身份
              let data = {
                'code': info.code,
                'source': 'dingtalk',
                // 调用新版的dingding api
                'initiatorFromApp': 'true'
              }
              // 准备发回调
              this.axios({
                method: 'post',
                url: '/api/ssoaly',
                data: qs.stringify(data)
              }).then(res => {
                // alert(JSON.stringify(res))
                let dduserid = res.data.dduserid
                let firstlogin = res.data.firstlogin
                if (firstlogin === 'true') {
                  this.$router.push({
                    name: 'home',
                    params: {
                      'dduserid': dduserid
                    }
                  })
                } else if (firstlogin === 'false') {
                  this.$router.push({
                    name: 'main'
                  })
                } else {
                  Toast('系统异常 请联系管理员')
                }
              }).catch(res => {
                this.reserror(res)
              })
            },
            onFail: (res) => {
              alert(JSON.stringify(res))
            }

          })
        })
      } else {
        // 钉钉消息
        // var urlparam = window.location.search.substr(1).match(new RegExp('(^|&)urlparm=([^&]*)(&|$)'))
        // alert('urlparam:' + window.location.href)

        let url = window.location.href

        url = url.substr(url.indexOf('urlparam=') + 9)
        let questionmarkIndex = url.indexOf('%3F')
        let pageName = url.substr(url.indexOf('%2F') + 3, questionmarkIndex - 3)
        //console.log('pageName=' + pageName)

        url = url.substr(questionmarkIndex)
        url = url.replace('%3F', '?')
        url = url.replace('%3D', '=')

        // alert('page=' + page +  ",url=" + url)
        const str = url.substr(url.indexOf('?') + 1)
        // arr每个元素都是完整的参数键值
        const arr = str.split('&')
        // result为存储参数键值的集合
        const result = {}
        for (let i = 0; i < arr.length; i++) {
          const item = arr[i].split('=')
          result[item[0]] = item[1]
        }
        this.$router.push({ name: pageName, query: result })
      }
    } else {
      // weixin 进不来,因为配置的地址不同
      // this.$router.push({
      //   name: 'home',
      //   params: {
      //     'wxuserid': this.wxuserid,
      //     'wxgzuserid': this.wxgzuserid
      //   }
      // })
      // 电脑端直接跳
      this.$router.push({ name: 'home' })
    }
  }
}
</script>

<style>
  .home {
    padding-top: 8vh;
    padding-left: 5vw;
    padding-right: 5vw;
  }
</style>
